/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

/**
 *
 * @author Bayu
 */
import com.bayu.services.dao.TRejectionMouldSm60Dao;
import com.bayu.services.entity.TRejectionMouldSm60;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/mould-input-sm60.htm")
public class MouldSm60Input {
    private TRejectionMouldSm60Dao dao;

    @Autowired
    public void setDao(TRejectionMouldSm60Dao dao) {
        this.dao = dao;

    }

    @RequestMapping(method=RequestMethod.GET)
    public void displayForm(ModelMap map, String id) {
    }

    @RequestMapping(method = RequestMethod.POST)
    public String proccessForm(@ModelAttribute("TRejectionMouldSm60") TRejectionMouldSm60 rejectionMould, BindingResult result,ModelMap map, String date,HttpServletRequest req){
            DateFormat formarter = new SimpleDateFormat("yy-MM-dd");
            Date tanggal;


            if(result.hasFieldErrors("date")){
                try {
                    tanggal = formarter.parse(date);
                    rejectionMould.setDate(tanggal);

                } catch (ParseException ex) {
                    Logger.getLogger(CompInput.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

             List total = this.dao.custom("select count(*) from TRejectionMouldSm60 where date='" + date + "'");

            String strTotal=total.get(0).toString();


            int c = Integer.valueOf(strTotal).intValue();
            System.out.println(strTotal+"---"+c+"--"+date);
            if(c > 0){
                return "redirect:ErrorDuplicate.html";
            }else{
                 this.dao.save(rejectionMould);
                 return "redirect:view-mould-sm60-data.htm?msg=Data has been updated";
            }

    }
}
